<?php
/**
 * | ---------------------------------------------------------------------------------------------------
 * | Author：johnxu <fsyzxz@163.com>.
 * | ---------------------------------------------------------------------------------------------------
 * | Home: https://www.johnxu.net.
 * | ---------------------------------------------------------------------------------------------------
 * | Data: 2019-03-16
 * | ---------------------------------------------------------------------------------------------------
 * | Desc: 分组管理
 * | ---------------------------------------------------------------------------------------------------
 */

namespace app\admin\controller;

use app\admin\model\UsersGroup;
use app\common\controller\Backend;
use app\admin\validate\Group as GroupValidate;
use app\admin\model\Group as GroupModel;

class Group extends Backend
{
    /**
     * 首页
     *
     * @return mixed
     */
    public function index()
    {
        $rows = GroupModel::all();

        return $this->fetch( '', [ 'rows' => $rows ] );
    }

    /**
     * 添加分组
     *
     * @return mixed
     */
    public function add()
    {
        if ( $this->request->isPost() )
        {
            $data          = $this->request->post();
            $groupValidate = new GroupValidate();
            if ( !$groupValidate->scene( 'add' )->check( $data ) )
            {
                $this->error( $groupValidate->getError() );
            }
            else
            {
                $result = GroupModel::create( $data );

                $result ? $this->success( '添加分组成功', url( 'index' ) ) : $this->error( '添加分组失败' );
            }
        }
        else
        {
            return $this->fetch();
        }
    }

    /**
     * 编辑功能
     *
     * @param string $uniqueId
     *
     * @return mixed
     */
    public function edit( string $uniqueId )
    {
        $row = GroupModel::get( [ 'unique_id' => $uniqueId ] );
        if ( !$row )
        {
            $this->error( '错误的分组编号' );
        }
        if ( $this->request->isPost() )
        {
            $data          = $this->request->post();
            $groupValidate = new GroupValidate();
            if ( !$groupValidate->scene( 'edit' )->check( $data ) )
            {
                $this->error( $groupValidate->getError() );
            }

            $row->title       = $data['title'];
            $row->description = $data['description'];
            $row->status      = $data['status'];
            $row->save() !== false ? $this->success( '编辑分组成功', url( 'index' ) ) : $this->error( '编辑分组失败' );
        }
        else
        {
            return $this->fetch( '', [ 'row' => $row ] );
        }
    }

    /**
     * 删除分组
     *
     * @param string $uniqueId
     */
    public function delete( string $uniqueId )
    {
        // 判断如果该分组下面有客服不能删除
        $row = GroupModel::get( [ 'unique_id' => $uniqueId ] );
        if ( !$row )
        {
            $this->error( '该分组不存在' );
        }
        if ( UsersGroup::get( [ 'group_id' => $row['id'] ] ) )
        {
            $this->error( '该分组下有客服，不能删除' );
        }
        $result = $row->delete();

        $result ? $this->success( '删除分组成功' ) : $this->error( '删除分组失败' );
    }
}
